/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Log;

import java.io.File;
import java.io.FileWriter;
import java.io.PrintWriter;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Calendar;

/**
 *
 * @author tomax
 */
public class Logger {
    private String Fname;
    private int id;
    
    public Logger(int ID,String type){

        //System.out.println("Iniciei Log Writter");
        this.id=ID;
        Calendar cal = Calendar.getInstance();
        SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy HH");
        int minutes = cal.get(Calendar.MINUTE);
        int hour = cal.get(Calendar.HOUR);
        int day = cal.get(Calendar.DAY_OF_MONTH);
        Fname= sdf.format(cal.getTime());
         try {
            boolean success = (new File("Logs" + File.separatorChar + day + "d" + hour + "h" + minutes + "m")).mkdirs();
             
             //System.out.println("Vou mostrar a success: " + success);
             //System.out.println("Tentativa de criar directorio: " + (new File("Logs\\" + hour + "-" + minutes)).mkdir());
           // if (!success)
           //     System.exit(0);
            
            String outputFileName = ("Logs" + File.separatorChar + day + "d" + hour + "h" + minutes + "m" +File.separatorChar + Fname + "_" + type + "_" + id + ".log");
            FileWriter outputFileReader  = new FileWriter(outputFileName,true);
            PrintWriter    outputStream  = new PrintWriter(outputFileReader);

            //outputStream.println("+---------- Inicio de Log da Aplicação ----------+");
            //outputStream.println();
            //outputStream.println();
            
            outputStream.close();
            Fname="Logs" + File.separatorChar + day + "d" + hour + "h" + minutes + "m" + File.separatorChar + Fname + "_" + type + "_" + id + ".log";
        } catch (IOException e) {
            System.out.println("IOException:");
            e.printStackTrace();
            
        }
    }
    public void Log(String Log){
        Calendar cal = Calendar.getInstance();
        SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");

        try {
            FileWriter outputFileReader  = new FileWriter(Fname,true);
            PrintWriter outputStream  = new PrintWriter(outputFileReader);

            outputStream.println(sdf.format(cal.getTime()) + " ---> " + Log);
            outputStream.println();

            outputStream.close();

        } catch (IOException e) {

            System.out.println("IOException:");
            e.printStackTrace();

        }
    }
}
